package com.baofu.paycore.biz.task;

import com.baofu.paycore.biz.notice.ProductHandler;
import com.baofu.paycore.biz.paycore.converter.BO2BOConverter;
import com.baofu.paycore.biz.paycore.handler.BaseHandler;
import com.baofu.paycore.biz.paycore.handler.impl.RechargeHandler;
import com.baofu.paycore.biz.paycore.handler.impl.WithdrawHandler;
import com.baofu.paycore.biz.tools.BuildModel;
import com.baofu.paycore.common.constant.PayCoreConstant;
import com.baofu.paycore.common.enums.BizCmdTypeEnum;
import com.baofu.paycore.service.facade.enums.ServiceCodeEnum;
import com.baofu.paycore.common.exception.PayCoreBizException;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.dal.model.ErrorLogDO;
import com.baofu.paycore.manager.model.*;
import com.baofu.paycore.manager.model.withdraw.WithdrawBO;
import com.baofu.paycore.manager.storage.impl.ErrorLogManager;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.enums.RequestSystemEnum;
import com.google.common.base.Splitter;
import com.system.dispatch.dal.model.BizCmdDO;
import com.system.dispatch.impl.BaseCmdHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.MDC;
import org.slf4j.helpers.SystemMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 调清分超时业务处理
 *
 * <p>
 *  1.获得执行任务名称
 *  2.执行任务
 *  3.任务执行最后失败信息
 * </p>
 * User: xin deng  Date: 16/7/6 ProjectName: paycore Version: 1.0
 */

@Slf4j
@Component
public class ClearTimeOutDispatch extends BaseCmdHandler {

    /**
     * 清分业务处理服务
     */
    @Autowired
    private ClearBizHandler clearBizHandler;

    /**
     * 差错日志Manager
     */
    @Autowired
    private ErrorLogManager errorLogManager;



    /**
     * 执行任务
     *
     * @param command 任务
     * @throws Exception 异常
     */
    @Override
    protected void doCmd(BizCmdDO command) throws Exception {
        clearBizHandler.process(command);
    }

    /**
     * 任务执行最后失败信息
     *
     * @param command 任务
     */
    @Override
    protected void failedFinally(BizCmdDO command) {

        log.error("清分超时处理 bizId:{},{}", command.getBizId(), command.getTraceLogId());
        List<String> strings = Splitter.on(PayCoreConstant.BASE_CMD_SEPARATOR).splitToList(command.getBizId());
        String requestType = strings.get(0);
        String payNo = strings.get(1);
        ErrorLogDO errorLogDO = BuildModel.buildErrorLog(payNo, null, BizCmdTypeEnum.CLEAR_TIME_OUT.getCode(),
                requestType, PayCoreErrorCode.DISPATCH_TIME_OUT_HANDLE_ERROR.getErrorCode(),
                command.getFailReason(), "清分超时dispatch处理失败");
        errorLogManager.addOrder(errorLogDO);
    }


}
